import 'package:flutter/material.dart'; // Material UI组件库
import 'package:fluttertoast/fluttertoast.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:shop/route/my_route.dart';
import 'dart:ui';

import 'package:shop/shop/life/orderPages/AllOfOrder.dart';
import 'package:shop/shop/life/orderPages/WanCheng.dart';
import 'package:shop/shop/life/orderPages/WeiFaHuo.dart';
import 'package:shop/shop/life/orderPages/WeiFuKuan.dart';
import 'package:shop/shop/life/orderPages/WeiShouHuo.dart';


class OrderListPage extends StatefulWidget {
  final String showIndex;
  OrderListPage({Key key, this.showIndex}) : super(key: key);
  @override
  State<StatefulWidget> createState() {
    return new _OrderListPage();
  }
}

class _OrderListPage extends State<OrderListPage> with SingleTickerProviderStateMixin {
  //构造方法
  _OrderListPage(){
    isLogin();
  }

  // 判断是否登录
  Future<void> isLogin() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    if(prefs.getBool("isLogin")==false){
      Fluttertoast.showToast(
          msg: "请先登录",
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.BOTTOM,
          timeInSecForIosWeb: 1,
          backgroundColor: Colors.black45,
          textColor: Colors.white,
          fontSize: 16.0
      );
    }
  }

  TabController mController;// tab控制器
  // int showIndex;
  // _OrderListPage(this.showIndex);
  final List<Tab> titleTabs = <Tab>[
    Tab(
      text: '全部',
    ),
    Tab(
      text: '待付款',
    ),
    // Tab(
    //   text: '待发货',
    // ),
    Tab(text: "待收货",),
    // Tab(text: "待自提",),
    Tab(text: "已收货",),

  ];

  @override
  void initState() {
    super.initState();

    mController = TabController(initialIndex: int.parse(widget.showIndex),length:titleTabs.length, vsync: this);
    // 初始化TabController
    // 参数1：初试显示的tab位置
    // 参数2：tab的个数
    // 参数3：动画效果的异步处理，默认格式

    // 添加监听器
    mController.addListener(() => _onTabChanged());

  }

  @override
  Widget build(BuildContext context) {
    return  new Scaffold(
      // 创建TabBar & 设置属性
      appBar: AppBar(
        title:Text("我的订单"),
        elevation: 0,
        bottom:TabBar(
          controller: mController,// 设置控制器
          labelColor: Colors.white, //选中的颜色
          labelStyle: TextStyle(fontSize: 16), //选中的样式
          unselectedLabelColor: Colors.black, //未选中的颜色
          unselectedLabelStyle: TextStyle(fontSize: 14), //未选中的样式
          indicatorColor: Colors.white, //下划线颜色
          isScrollable: true, //是否可滑动，设置不可滑动，则是tab的宽度等长
          //tab标签
          tabs: titleTabs, // 设置标题
          //点击事件
          onTap: (int i) {
            print(i+10);
          },
        ) ,
      ),
      body: Container(
        padding: EdgeInsets.only(top:20),
        child: new TabBarView(
        controller: mController,
        children:  <Widget>[ // 每个空间对应的页面
          AllOfOrder(titleTabs),
          WeiFuKuan(titleTabs),
          // WeiFaHuo(titleTabs),
          WeiShouHuo(titleTabs),
          // ZiTi(titleTabs),
          WanCheng(titleTabs)
        ],
      )
        ,)
    );
  }

  @override
  void dispose() {
    super.dispose();
    mController.dispose(); // 当整个页面dispose时，记得把控制器也dispose掉，释放内存
  }

  // 点击监听函数
  _onTabChanged() {
    // print(mController.index);
  }
}